package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.MyProduct;
import com.kbbywyclient.data.entity.ProductLib;
import com.kbbywyclient.data.model.LoansTableMenuModel;
import com.kbbywyclient.data.model.MyproductInfoModel;
import com.kbbywyclient.data.model.ProductLibInfoModel;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.ProductManageContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.customservice.ApiException;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Description:
 * Created by FHW
 * on 2017/5/10
 */

public class ProductManagePresenter extends BasePresenter implements ProductManageContract.Presenter {

    private ProductManageContract.View view;
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    private int pageno = 1;
    private List<MyProduct> myProductList = null;

    private int pageno_lib = 1;
    private List<ProductLib> productLibList = null;


    @Inject
    public ProductManagePresenter(ProductManageContract.View view) {
        this.view = view;
        myProductList = new ArrayList<>();
        this.view.setAdapterOrNotify(myProductList);
        productLibList = new ArrayList<>();
        this.view.setAdapterOrNotifyLib(productLibList);
    }

    @Override
    public void getMyproductList(boolean isClean, String SalesID, String IsVerified, String KeyWord) {
        if (isClean) {
            pageno = 1;
        }
        rsaJsonUtil.put("pageno", pageno + "");
        rsaJsonUtil.put("pageSize", API.PAGE_SIZE);
        rsaJsonUtil.put("SalesID", SalesID);
//        if (StringUtil.isNotEmpty(IsVerified)) {
        rsaJsonUtil.put("IsVerified", IsVerified);
//        }
        if (StringUtil.isNotEmpty(KeyWord)) {
            rsaJsonUtil.put("KeyWord", KeyWord);
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getMyproductList(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<MyproductInfoModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.loadFaiMyproduct(e.toString());
                    }

                    @Override
                    public void onNext(MyproductInfoModel myproductInfoModel) {
                        if (isClean) {
                            myProductList.clear();
                        }
                        myProductList.addAll(myproductInfoModel.getData().getResult());
                        view.setAdapterOrNotify(myProductList);//刷新
                        view.loadSucMyproduct(myProductList);
                        //分页
                        if (myproductInfoModel.getData().getPaging().getCurrentPage() >= myproductInfoModel.getData().getPaging().getTotalPage()) {
//                            if (myproductInfoModel.getData().getResult().size() > 1) {
                            view.setNoMore();
//                            }
                        } else {
                            pageno++;
                            view.setMoreComplete();
                        }
                    }
                });

    }

    @Override
    public void getMyRecommend(String SalesID, String ProductLoansID, String Recommend) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("ProductLoansID", ProductLoansID);
        rsaJsonUtil.put("Recommend", Recommend);
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getMyRecommend(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<BaseModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(BaseModel baseModel) {
                        view.loadSucMyRecommend(baseModel);
                    }
                });
    }

    @Override
    public void getMyDel(String SalesID, String ProductLoansID) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("ProductLoansID", ProductLoansID);
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getMyDel(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<BaseModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(BaseModel baseModel) {
                        view.loadSucMyDel(baseModel);
                    }
                });

    }

    @Override
    public void getProductLib(boolean isClean, String SalesID, String ParentUsage, String KeyWord) {
        if (isClean) {
            pageno_lib = 1;
        }
        rsaJsonUtil.put("pageno", pageno_lib + "");
        rsaJsonUtil.put("pageSize", API.PAGE_SIZE);
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("ParentUsage", ParentUsage);
        if (StringUtil.isNotEmpty(KeyWord)) {
            rsaJsonUtil.put("KeyWord", KeyWord);
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getProductLib(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<ProductLibInfoModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.loadFaiProductLib(e.toString());
                        if (e instanceof ApiException) {

                        } else {
                            HttpExcUtil.printError(e);
                        }
                    }

                    @Override
                    public void onNext(ProductLibInfoModel productLibInfoModel) {
                        if (isClean) {
                            productLibList.clear();
                        }
                        productLibList.addAll(productLibInfoModel.getData().getResult());
                        view.setAdapterOrNotifyLib(productLibList);//刷新
                        view.loadSucProductLib(productLibList);
                        //分页
                        if (productLibInfoModel.getData().getPaging().getCurrentPage() >= productLibInfoModel.getData().getPaging().getTotalPage()) {
//                            if (productLibInfoModel.getData().getResult().size() > 1) {
                            view.setNoMoreLib();
//                            }
                        } else {
                            pageno_lib++;
                            view.setMoreCompleteLib();
                        }
                    }
                });
    }

    @Override
    public void getloansOrdersMenu(String SalesID) {
        rsaJsonUtil.put("SalesID", SalesID);
        ApiServiceHelper.
                appointThreadNormal(apiService.getloansOrdersMenu(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<LoansTableMenuModel>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(LoansTableMenuModel loansTableMenuModel) {
                        view.loadSucMenu(loansTableMenuModel.getData());
                    }
                });

    }

    @Override
    public void getProductAdd(String SalesID, String ProductLoansID) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("ProductLoansID", ProductLoansID);
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getProductAdd(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<BaseModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(BaseModel baseModel) {
                        view.loadSucAdd(baseModel);
                    }
                });
    }

}
